package icom.acm.action;
/**
 * @author shench
 * 用户关系动作类
 */
import icom.acm.bean.T_USER;
import icom.acm.bean.T_USERRELATION;
import icom.acm.manager.T_UserRelationMgr;
import icom.acm.util.DBConnect;
import icom.acm.util.json.JSONMsg;

import java.io.UnsupportedEncodingException;
import java.util.List;


public class UserRelationAction extends Action {
	private T_USERRELATION ur;

	/**
	 * 关注某人
	 */
	public void attention(){
		JSONMsg jmsg = null;
		T_USER u = this.checkUser();
		if (u==null){
			jmsg = new JSONMsg(false, "用户未登录!");
			out.print(jmsg.toString());			
			return ;
		}
		String u_name = null;
		try {
			u_name = new String(request.getParameter("u_name").getBytes("ISO-8859-5"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			jmsg = new JSONMsg(false, "未知的错误!");
			out.print(jmsg.toString());
			e.printStackTrace();
		}
		
		T_UserRelationMgr urmgr = new T_UserRelationMgr();
		DBConnect db = new DBConnect();
		
		ur.setUR_U_ID(u.getU_ID());		// 关注者
		try {
			List<T_USERRELATION> list = urmgr.selectByU_UED(ur, db);
			if (list.size()>0)	{	// 表示没有关注该用户
				jmsg = new JSONMsg(false, "你已经关注了该用户!");
				out.print(jmsg.toString());
				return ;
			}
			
			if (urmgr.insert(ur, db)){
				jmsg = new JSONMsg(true, "成功关注了用户 "+u_name+" !");
				out.print(jmsg.toString());	
			} else {
				jmsg = new JSONMsg(false, "未知的错误!");
				out.print(jmsg.toString());
				return ;
			}			
		} catch (Exception e) {
			jmsg = new JSONMsg(false, "未知的错误!");
			out.print(jmsg.toString());
			e.printStackTrace();
			return ;
		} finally {
			db.close();
		}
	}
	
	public T_USERRELATION getUr() {
		return ur;
	}

	public void setUr(T_USERRELATION ur) {
		this.ur = ur;
	}
	
	
}
